package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 功能：提供接口返回数据
 * 作者：zhoujun
 * 日期： 2024/4/23 0:13
 */
@RestController

public class WebController {

    @Autowired
    UserService userService;

    @AuthAccess
   @GetMapping("/")
    public Result hello(String name){
    return Result.success("success");
    }

    @HoneyLogs(operation = "用户", type = LogType.LOGIN)
    @PostMapping("/login")
    public Result login(@RequestBody User user){
       if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword()))
       {
           return Result.error("数据输入不合法！");
       }
    user = userService.login(user);
       return Result.success(user);
    }

    @HoneyLogs(operation = "用户", type = LogType.REGISTER)
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword()) || StrUtil.isBlank(user.getRole()))
        {
            return Result.error("数据输入不合法！");
        }

        if(user.getUsername().length() > 10 || user.getPassword().length() > 20)
        {
            return Result.error("数据输入不合法！");
        }

        user = userService.register(user);
        return Result.success(user);
    }

    @HoneyLogs(operation = "密码", type = LogType.UPDATE)
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPhone())){
            return Result.error("数据输入不合法！");
        }
        userService.resetPassword(user);
        return Result.success();
    }



}